package com.ssh.manager.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.Brand;
import com.ssh.entity.ChangGui;
import com.ssh.entity.Company;
import com.ssh.entity.HelpSale;
import com.ssh.entity.SuperMarket;
import com.ssh.manager.ChangGuiManager;

@Service
public class ChangGuiManagerImpl implements ChangGuiManager {
	@Resource
	private IDao daoImpl;
	@Override
	public ChangGui saveUpdate(ChangGui changGui) {
		return daoImpl.saveOrUpdateDomain(changGui);
	}

	@Override
	public boolean delete(Long id) {
		try {
			ChangGui c = daoImpl.getDomain(ChangGui.class, id);
			c.setDel(1);
			daoImpl.saveOrUpdateDomain(c);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public ChangGui getById(Long id) {
		return daoImpl.getDomain(ChangGui.class, id);
	}

	@Override
	public void findList(Map conditionMap, Page page, String isDesc) {
		String sql = "select count(*) from ChangGui where del!=1";
		String findSql = "from ChangGui where del!=1";
		if(conditionMap.get("employee")!=null && !conditionMap.get("employee").equals("")){
			sql += " and taskProductEmployee.employee.id="+conditionMap.get("employee");
			findSql += " and taskProductEmployee.employee.id="+conditionMap.get("employee");
		}
		if(conditionMap.get("task")!=null && !conditionMap.get("task").equals("")){
			sql += " and taskProductEmployee.taskBrand.task.name like '%"+conditionMap.get("task")+"%'";
			findSql += " and taskProductEmployee.taskBrand.task.name like '%"+conditionMap.get("task")+"%'";
		}
		if(conditionMap.get("market")!=null && !conditionMap.get("market").equals("")){
			sql += " and taskProductEmployee.superMarketProduct.superMarket.id="+conditionMap.get("market");
			findSql += " and taskProductEmployee.superMarketProduct.superMarket.id="+conditionMap.get("market");
		}
		
		int sum = daoImpl.countAll(sql, null);
		page.setTotalCount(new Long(sum));
		page.countTotalPageNum();
		
		findSql += " order by createDate "+isDesc;
		List<ChangGui> changGuis = (List<ChangGui>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
		page.setPageData(changGuis);
	}
	
	  @SuppressWarnings("unchecked")
	public List<Object> tongjiByTotal(Timestamp date,String groupString){
		 String sql="select  c.taskProductEmployee.superMarketProduct.product.brand.name,sum(c.taskProductEmployee.planStock),sum(c.stock) from ChangGui c where c.createDate>=?";
		  sql+="  group by c.taskProductEmployee.superMarketProduct."+groupString+" order by c.createDate  desc";
		 try{
		  List<Object[]> changGuis = (List<Object[]>)daoImpl.findListBySql(sql, new Object[]{date}, false, true, false,0,-1);
		  List<Object> result=new ArrayList<Object>();
		  for(Object[] object:changGuis){
			  Map<String, String> map=new HashMap<String, String>();
			  map.put("name", object[0].toString());
			  map.put("planStock", object[1].toString());
			  map.put("stock", object[2].toString());
			  result.add(map); 
		  }
		  return result;
		 }catch (Exception e) {
			 e.printStackTrace();
		}
		  return null;
	  }

	    public List<Object> tongjiByProduct(Timestamp date,int brandId,int companyId,int superMarketId){
	    	 String sql="select  c.taskProductEmployee.taskBrand.brand.name,c.taskProductEmployee.superMarketProduct.product.pinLei,c.taskProductEmployee.superMarketProduct.storecode,c.taskProductEmployee.superMarketProduct.product.barcode,c.taskProductEmployee.superMarketProduct.product.name,sum(c.taskProductEmployee.planStock),sum(c.stock) from ChangGui c where c.createDate>='"+date+"'";
			 List<Object> objects=new ArrayList<Object>();
	    	 if (brandId>0) {
				sql+=" and c.taskProductEmployee.taskBrand.brand.id="+brandId+" ";
				//objects.add(String.valueOf(brandId));
			}
			if (companyId>0) {
				sql+= " and c.taskProductEmployee.superMarketProduct.superMarket.company.id="+companyId;
				//objects.add(String.valueOf(companyId));
			}
			if (superMarketId>0) {
				sql+= " and c.taskProductEmployee.superMarketProduct.superMarket.id="+superMarketId;
				//objects.add(String.valueOf(superMarketId));
			}
	    	 sql+="  group by c.taskProductEmployee.superMarketProduct.product.id order by c.createDate  desc";
			 try{
			  List<Object[]> changGuis = (List<Object[]>)daoImpl.findListBySql(sql, objects, false, true, false,0,-1);
			  List<Object> result=new ArrayList<Object>();
			  for(Object[] object:changGuis){
				  Map<String, String> map=new HashMap<String, String>();
				  map.put("brandName", object[0].toString());
				  map.put("pinLei", object[1].toString());
				  map.put("storecode", object[2].toString());
				  map.put("barcode", object[3].toString());
				  map.put("name", object[4].toString());
				  map.put("planStock", object[5].toString());
				  map.put("stock", object[6].toString());
				  result.add(map); 
			  }
			  return result;
			 }catch (Exception e) {
				 e.printStackTrace();
			}
		  return null;
		
	  }
	    public Object find_BrandCompanySuperMarket(){
	    	 Map<String, Object> map=new HashMap<String, Object>();
	    	List<Brand> brandList=(List<Brand>)daoImpl.findListBySql("from Brand", new Object[]{}, false, true, false,0,-1);
	    	List<Company> companyList=(List<Company>)daoImpl.findListBySql("from Company", new Object[]{}, false, true, false,0,-1); 
	    	List<SuperMarket> superMarketList=(List<SuperMarket>)daoImpl.findListBySql("from SuperMarket", new Object[]{}, false, true, false,0,-1); 
	  	   map.put("brandList", brandList);
	  	   map.put("companyList", companyList);
	  	   map.put("superMarketList", superMarketList);
	  	   return map;
	    }
	    
	    public List<ChangGui> listForChangGui(Timestamp startDate,Timestamp endDate,long superMarketId,long companyId){
	    	List<Object> param=new ArrayList<Object>();
	    	String sqlString="from ChangGui where createDate>=? and createDate<=?";
	    	param.add(startDate);
	    	param.add(endDate);
	    	if (superMarketId>0) {
	    		param.add(superMarketId);
	    	 sqlString=sqlString+"  and taskProductEmployee.superMarketProduct.superMarket.id=?";
	    	}
	    	if (companyId>0) {
	    		param.add(companyId);
	    	 sqlString=sqlString+"  and taskProductEmployee.superMarketProduct.superMarket.company.id=?";
	    	}
	    	sqlString=sqlString+" " +
	    			"group by taskProductEmployee.superMarketProduct.product.id"; 
	    	//System.out.print(sqlString);
	    	return (List<ChangGui>)daoImpl.findListBySql(sqlString,param, false, true, false, 0, -1);
	    } 
	    public List<HelpSale> listForHelpSale(Timestamp startDate,Timestamp endDate,long superMarketId,long companyId){
	    	List<Object> param=new ArrayList<Object>();
	    	String sqlString="from HelpSale where createDate>=? and createDate<=?";
	    	param.add(startDate);
	    	param.add(endDate);
	    	if (superMarketId>0) {
	    		param.add(superMarketId);
	    	 sqlString=sqlString+"  and taskProductEmployee.superMarketProduct.superMarket.id=?";
	    	}
	    	if (companyId>0) {
	    		param.add(companyId);
	    	 sqlString=sqlString+"  and taskProductEmployee.superMarketProduct.superMarket.company.id=?";
	    	}
	    	sqlString=sqlString+" " +
	    			"group by taskProductEmployee.superMarketProduct.product.id"; 
	    	//System.out.print(sqlString);
	    	return (List<HelpSale>)daoImpl.findListBySql(sqlString,param, false, true, false, 0, -1);
	    } 
}
